package modules;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import main.TankSicherheit;

import web.interfaces.ClanwarLineupReceiverDB;
import web.interfaces.HTTPPostProcessingTankSicherheitModul;
import data.WoTCWChannel;

public class ClanwarLineupReceiver implements HTTPPostProcessingTankSicherheitModul {
	private ClanwarLineupReceiverDB database;
	private boolean running=false;
	public ClanwarLineupReceiver(ClanwarLineupReceiverDB db)
	{
		database=db;
		
	}
	
	@Override
	public int processHTTPPost(int fc,Map<String, String[]> req) {
		TankSicherheit.LOGGER.debug("processing ClanwarLineup");
		try
		{
			System.out.println("###############");
			for(String line:req.keySet())
			{
				System.out.println(line + "="+req.get(line)[0]);
			}
			
			
			
			WoTCWChannel cw = new WoTCWChannel();
			
			//channelid
			String channelid = req.get("channelid")[0];
			cw.channelid = Integer.parseInt(channelid);
			
			//map
			cw.map = req.get("map")[0];
			
			//enemy
			cw.enemy = req.get("enemy")[0];
			
			
			TankSicherheit.LOGGER.debug("parsing lineup");
			//lineupamount
			int lineupamount = Integer.parseInt(req.get("lineupamount")[0]);
			
			
			
			
			
			//lineup
			String lineup = req.get("lineup")[0];
			System.out.println("lineup:"+lineup);
			String[] lu = lineup.split("\\|");
			List<Integer> lineuplists = new ArrayList<Integer>(15);
			for(String l:lu)
			{
				if(!l.isEmpty())
				{
					
					lineuplists.add(Integer.parseInt(l));
				}
				
			}
			
			
			TankSicherheit.LOGGER.debug("parsing reserve");
			int rosteramount = Integer.parseInt(req.get("reserveamount")[0]);
			
			//roster
			String roster = req.get("reserve")[0];
			String[] ro = lineup.split("\\|");
			List<Integer> rosterlists = new ArrayList<Integer>(15);
			for(String r:ro)
			{
				if(r.isEmpty())
				{
					rosterlists.add(Integer.parseInt(r));
				}
				
			}
			database.insertClanwar(fc,cw, lineuplists, lineupamount,rosterlists,rosteramount);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return -2;

	}

	@Override
	public void startup() {
		running=true;
		
	}

	@Override
	public void shutdown() {
		running=false;
		
	}

	@Override
	public boolean isRunning() {
		return running;
	}

}
